<?php

namespace App\Http\Requests\Api;

class AuthRequest extends FormRequest
{
    public function rules()
    {
        $action = get_current_action()['action'];

        $rules = [
            'user_mobile' => [
                'required',
                'exists:ucenter_user_info_detail',
                'regex:/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/'
            ],
        ];

        switch ($action) {
            case 'loginByPassword':
                return array_merge($rules, ['password' => 'required']);
            case 'loginByCode':
                return array_merge($rules, ['sms_key' => 'required', 'sms_code' => 'required']);
            case 'loginBySocial':
                return [
                    'openid' => 'required',
                    'nickname' => 'required',
                    'avatar' => 'required',
                    'gender' => 'required',
                    'access_token' => 'required',
                    'type' => 'required|in:qq,weixin,aplipay'
                ];
            case 'signUp':
                $rules['user_mobile'][1] = 'unique:ucenter_user_info_detail';
                return array_merge($rules, [
                    'sms_key' => 'required',
                    'sms_code' => 'required',
                    'password' => 'required|min:6'
                ]);
            default:
                return [];
        }
    }

    public function messages()
    {
        return [
            'user_mobile.required' => '手机号不能为空',
            'user_mobile.regex' => '手机号错误',
            'user_mobile.exists' => '手机号不存在',
            'user_mobile.unique' => '手机号已存在',
            'password.required' => '密码不有为空',
            'password.min' => '密码最少 6 位',
            'sms_key.required' => '验证码标识不能为空',
            'sms_code.required' => '验证码不能为空',
            'openid.required' => 'openid 不能为空',
            'nickname.required' => 'nickname 不能为空',
            'avatar.required' => 'avatar 不能为空',
            'type.required' => '三方授权登录类型不能为空',
            'type.in' => '目前只支持QQ、微信和支付宝登录'
        ];
    }
}
